<!--
 * @Author: Wangtao
 * @Date: 2021-04-01 11:41:07
 * @LastEditors: Wangtao
 * @LastEditTime: 2021-04-19 19:43:05
-->
<template>
  <cardLayout>
    <template slot="card">
      <webchatCardGroup></webchatCardGroup>
    </template>
    <template slot="detail">
      <keep-alive>
        <router-view></router-view>
      </keep-alive>
    </template>
  </cardLayout>
</template>
<script lang='ts'>
import { Component, Vue } from 'vue-property-decorator'
import cardLayout from '@components/cardLayout/index.vue'
import webchatCardGroup from '@/views/webchat/webchatCardGroup.vue'
import { WebchatModule } from '@/store/modules/webchat'
@Component({
  name: 'webchat',
  components: { cardLayout, webchatCardGroup }
})
export default class Webchat extends Vue {
  private currentRoute: string = ''

  private beforeRouteLeave (to: any, from: any, next: any) {
    this.currentRoute = from.fullPath
    next()
  }

  private beforeRouteEnter (to: any, from: any, next: any) {
    next((vm: any) => {
      if (to.params.id && to.params.id !== '') {
        if (['webchat_leaveMessage', 'webchat_close', 'webchat_queue'].includes(to.params.activeName)) { // 刷新时如果在留言或者已关闭tab 上时需要跳转到待处理tab 上
          vm.$router.push({ path: '/im_workBench/index/webchat_todo/empty' })
        }
        // 从会话卡片跳出
      } else if (vm.currentRoute) {
        // 点击其他模块回到当前模块离开时的模块 || 刷新系统
        const sessionId = vm.currentRoute.split('/')[4]
        const type = vm.currentRoute.split('/')[3] || ''
        if (WebchatModule.webchatList[type]) { // 判断当前要跳转的会话是否纯在会话列表中
          const currentSession = WebchatModule.webchatList[type].find((item: any) => {
            return item._id === sessionId
          })
          if (currentSession) {
            vm.$router.push({ path: vm.currentRoute })
          } else {
            vm.$router.push({ path: `/im_workBench/index/${type}/empty` })
          }
        }
      }
    })
  }
}
</script>
